import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LogoutServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username=null;
        String password=null;
        //通过创建和之前同名的cookie，利用出现同名cookie后者会代替前者的机制，再将cookie设置有效时间为0，则达到了“销毁”cookie的作用。
        //这里主要是用户在点击退出系统后，会销毁之前用户点击的“三天内免登录”cookie
        // 避免出现免登录期间他人恶意操作的情况。
        Cookie cookie1=new Cookie("username",username);
        Cookie cookie2=new Cookie("password",password);
        cookie1.setMaxAge(0);
        cookie2.setMaxAge(0);
        cookie1.setPath(request.getContextPath());
        cookie2.setPath(request.getContextPath());
        response.addCookie(cookie1);
        response.addCookie(cookie2);
//        Cookie[] cookies=request.getCookies();
//        if (cookies==null){
//            response.sendRedirect(request.getContextPath()+"/LogoutSuccess.html");
//        }
        response.sendRedirect(request.getContextPath()+"/LogoutSuccess.html");
    }
}
